<template>
    <div v-if="isExternals" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-bind="$attrs" />
    <svg v-else :class="svgClass" aria-hidden="true" v-bind="$attrs">
        <use :xlink:href="iconName" />
    </svg>

</template>

<script setup lang="ts" name="SvgIcon">
import { isExternal } from '@/utils/validate'
import { computed } from 'vue'
const props = defineProps({
    // prefix: {
    //   type: String,
    //   default: 'icon'
    // },
    // name: {
    //   type: String,
    //   required: true
    // },
    iconClass: {
        type: String,
        required: true
    },
    className: {
        type: String,
        default: ''
    }
})
const isExternals = computed(() => isExternal(props.iconClass))
const iconName = computed(() => `#icon-${props.iconClass}`)
const svgClass = computed(() => {
    if (props.className) {
        return 'svg-icon ' + props.className
    } else {
        return 'svg-icon'
    }
})
const styleExternalIcon = computed(() => {
    return {
        mask: `url(${props.iconClass}) no-repeat 50% 50%`,
        '-webkit-mask': `url(${props.iconClass}) no-repeat 50% 50%`
    }
})
</script>
<style scope>
.svg-icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.1em;
    /* 因icon大小被设置为和字体大小一致，而span等标签的下边缘会和字体的基线对齐，故需设置一个往下的偏移比例，来纠正视觉上的未对齐效果 */
    fill: currentColor;
    /* 定义元素的颜色，currentColor是一个变量，这个变量的值就表示当前元素的color值，如果当前元素未设置color值，则从父元素继承 */
    overflow: hidden;
}
</style>
